<?php

App::uses('AppController', 'Controller');

/**
 * Movimientos Controller
 *
 * @property Movimiento $Movimiento
 * @property PaginatorComponent $Paginator
 */
class MovimientosController extends AppController
{
    public $layout = 'recargas';
    /**
     * Components
     *
     * @var array
     */
    //public $components = array('Paginator');
    public $uses = array('Movimiento');

    /**
     * index method
     *
     * @return void
     */
    public function index()
    {
        //$this->Movimiento->recursive = 0;
        //$this->set('movimientos', $this->Paginator->paginate());
        $movimientos = $this->Movimiento->find('all', array(
            'recursive'=>0,
            'limit'=>50,
            'order'=>'Movimiento.id DESC'
        ));
        $this->set(compact('movimientos'));
    }    

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null)
    {
        if (!$this->Movimiento->exists($id))
        {
            throw new NotFoundException(__('Invalid movimiento'));
        }
        $options = array('conditions' => array('Movimiento.' . $this->Movimiento->primaryKey => $id));
        $this->set('movimiento', $this->Movimiento->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add()
    {
        if ($this->request->is('post'))
        {
            $this->Movimiento->create();
            if ($this->Movimiento->save($this->request->data))
            {
                return $this->flash(__('The movimiento has been saved.'), array('action' => 'index'));
            }
        }
        $pedidos = $this->Movimiento->Pedido->find('list');
        $users = $this->Movimiento->User->find('list');
        $porcentajes = $this->Movimiento->Porcentaje->find('list');
        $this->set(compact('pedidos', 'users', 'porcentajes'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null)
    {
        if (!$this->Movimiento->exists($id))
        {
            throw new NotFoundException(__('Invalid movimiento'));
        }
        if ($this->request->is(array('post', 'put')))
        {
            if ($this->Movimiento->save($this->request->data))
            {
                return $this->flash(__('The movimiento has been saved.'), array('action' => 'index'));
            }
        } else
        {
            $options = array('conditions' => array('Movimiento.' . $this->Movimiento->primaryKey => $id));
            $this->request->data = $this->Movimiento->find('first', $options);
        }
        $pedidos = $this->Movimiento->Pedido->find('list');
        $users = $this->Movimiento->User->find('list');
        $porcentajes = $this->Movimiento->Porcentaje->find('list');
        $this->set(compact('pedidos', 'users', 'porcentajes'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function delete($id = null)
    {
        $this->Movimiento->id = $id;
        if (!$this->Movimiento->exists())
        {
            throw new NotFoundException(__('Invalid movimiento'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->Movimiento->delete())
        {
            return $this->flash(__('The movimiento has been deleted.'), array('action' => 'index'));
        } else
        {
            return $this->flash(__('The movimiento could not be deleted. Please, try again.'), array('action' => 'index'));
        }
    }
    
    public function reversion()
    {
        if($this->request->is('post'))
        {
            $montoARevertir = $this->request->data['Movimiento']['monto'];
            debug($this->request->data);
            $ultimoMonto = $this->Movimiento->find('first', array(
                'recursive'=>-1,
                'order'=>'Movimiento.id DESC'
            ));
            $idUsuario = $this->Session->read('Auth.User.id');
            
            $totalMonto = $ultimoMonto['Movimiento']['total'];
            $montoRevertido = $totalMonto + $montoARevertir;
            $this->request->data['Movimiento']['ingreso']=$montoARevertir;
            $this->request->data['Movimiento']['total']=$montoRevertido;
            $this->request->data['Movimiento']['user_id']=$idUsuario;
            $this->Movimiento->create();
            if($this->Movimiento->save($this->request->data)){
                $this->Session->setFlash('EL monto se revertio correctamente', 'msgbueno');
                $this->redirect(array('controller'=>'pedidos', 'action'=>'clientes'));
            }
            //debug($ultimoMonto);die;
        }
    }
    public function modalreversion()
    {
        $this->layout = 'ajax';
    }
}
